package com.zlyx.easy.swagger.request;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.zlyx.easy.core.annotations.SpringBean;
import com.zlyx.easy.core.model.ResultModel;
import com.zlyx.easy.core.utils.LogUtils;
import com.zlyx.easy.log.process.AspectProcess;

@SpringBean(todo = { "请求过滤器" })
public class RequestFilter implements Filter {

	@Autowired(required = false)
	private AspectProcess aspectProcess;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		try {
			chain.doFilter(request, response);
		} catch (Exception e) {
			if (aspectProcess == null) {
				response.getWriter().print(ResultModel.err(e));
			} else {
				aspectProcess.onErr(response, e);
			}
			LogUtils.err(e);
		}
	}

	@Override
	public void destroy() {

	}
}
